package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.EmergencyEquipment;
import com.system.runtai.entity.EmergencyVehicle;
import com.system.runtai.service.EmergencyEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 应急装备 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-05
 */
@RestController
@Api(tags = "应急装备")
@RequestMapping("/runtai/emergency-equipment")
public class EmergencyEquipmentController {

    @Autowired
    private EmergencyEquipmentService emergencyEquipmentService;


    @ApiOperation(value = "所有应急装备")
    @GetMapping("/emergencyEquipmentList")
    public Result<List<EmergencyEquipment>> emergencyEquipmentList(){
        List<EmergencyEquipment> list = emergencyEquipmentService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "所有应急装备")
    @GetMapping("/emergencyEquipment/{id}")
    public Result<List<EmergencyEquipment>> emergencyEquipmentById(@PathVariable Integer id){
        List<EmergencyEquipment> list = emergencyEquipmentService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加应急装备")
    @PostMapping("/saveEmergencyEquipment")
    public Result<EmergencyEquipment> saveEmergencyEquipment(@RequestBody EmergencyEquipment emergencyEquipment){

        emergencyEquipment.setCreateTime(LocalDateTime.now());
        boolean save = emergencyEquipmentService.save(emergencyEquipment);

        if (save){
            return Result.ok(emergencyEquipment).message("添加成功");
        }else {
            return Result.ok(emergencyEquipment).message("添加失败");
        }
    }

    @ApiOperation(value = "删除应急装备")
    @PostMapping("delectEmergencyEquipment/{id}")
    public Result delectEmergencyEquipment(@PathVariable Integer id){

        boolean b = emergencyEquipmentService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "修改应急装备")
    @PostMapping("/updateEmergencyEquipment")
    public Result<EmergencyEquipment> updateEmergencyEquipment(@RequestBody EmergencyEquipment emergencyEquipment){

        emergencyEquipment.setUpdateTime(LocalDateTime.now());
        boolean update = emergencyEquipmentService.updateById(emergencyEquipment);

        if (update){
            return Result.ok(emergencyEquipment).message("修改成功");
        }else {
            return Result.ok(emergencyEquipment).message("修改失败");
        }
    }

    @ApiOperation(value = "分页查询应急装备")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<EmergencyEquipment>> index465(@PathVariable Long page, @PathVariable Long limit, EmergencyEquipment emergencyEquipment){

        Page<EmergencyEquipment> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<EmergencyEquipment> wrapper = new LambdaQueryWrapper<>();

        Integer id = emergencyEquipment.getId();

        if (id!=null){
            wrapper.eq(EmergencyEquipment::getId,id);
        }

        IPage<EmergencyEquipment> iPage = emergencyEquipmentService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

    @ApiOperation(value = "应急装备名称筛选")
    @GetMapping("/filkte")
    public Result<IPage<EmergencyEquipment>> filtecarName(@RequestParam(required = false) String carName,@RequestParam(required = false) Integer limit,@RequestParam(required = false) Integer page){

        List<EmergencyEquipment> list = null;
        if (carName!=null) {
            LambdaQueryWrapper<EmergencyEquipment> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(EmergencyEquipment::getCarname, carName);
             list = emergencyEquipmentService.list(wrapper);
            Page<EmergencyEquipment> recommendPage = new Page<>(page,limit);
            IPage<EmergencyEquipment> iPage = emergencyEquipmentService.page(recommendPage,wrapper);
            return Result.ok(iPage);
        }else if (carName==null){
           list = emergencyEquipmentService.list();

            Page<EmergencyEquipment> recommendPage = new Page<>(page,limit);
            IPage<EmergencyEquipment> iPage = emergencyEquipmentService.page(recommendPage);
            return Result.ok(iPage);
        }

        Page<EmergencyEquipment> recommendPage = new Page<>(page,limit);
        IPage<EmergencyEquipment> iPage = null;
        return Result.fail(iPage);
    }

}

